package com.zhq.blog.manage.domain.controller.safe;


import javax.validation.Valid;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.zhq.blog.core.domain.admin.Operator;
import com.zhq.blog.manage.domain.controller.BaseController;
import com.zhq.blog.manage.domain.controller.BossModel;

@Controller
@Scope("prototype")
@RequestMapping("/admin/safe/operator/*")
public class OperatorController extends BaseController {
	
	@RequestMapping()
    public BossModel index(){
		return createModel("admin/safe/list").add("operators",context.getOperatorRepository().findAll());
	}
	
	@RequestMapping(value="add")
	public BossModel add(){
		return createOperatorModelInfo(new Operator());
	}
	
	@RequestMapping(value="edit/{id}")
	public BossModel edit(@PathVariable long id){
		
		return createOperatorModelInfo(new Operator().findOne(id));
	}
	
	@RequestMapping(value="delete",method= RequestMethod.POST)
	public BossModel delete(long id){
		context.getOperatorRepository().delete(id);
		return createModel("admin/safe/operator_table").add("selectRecommends", Operator.SelectRecommend.values()).add("operators",context.getOperatorRepository().findAll());
	}
	
	@RequestMapping(value="save",method = RequestMethod.POST)
	@InitBinder(value = { "loginName", "password","selectRecommends" })
	public BossModel save(@Valid final Operator operator, final BindingResult bindingResult, final ModelMap model, RedirectAttributes redirectPrizes){
		if (bindingResult.hasErrors()) {
			return createOperatorModelInfo(new Operator());
		}
		//context.getOperatorRepository().save(operator);
		operator.save(operator);
		System.err.println(context.getOperatorRepository().findAll().get(0).getSelectRecommends());
		String message = operator.getSucessFlashMessage();
		redirectPrizes.addFlashAttribute("message", message);
		return createModel("redirect:/admin/safe/operator/index");
	}
	
	@RequestMapping(value = "verifyName")
	public @ResponseBody boolean verifyOnlyName(Operator operator, String operatorId) {
		int num = context.getOperatorRepository().findByName(operator.getName()).size();
		if (num != 0) {
			if (validation(operatorId)) {
				if (context.getOperatorRepository().findOne(Long.parseLong(operatorId)).getName().equals(operator.getName())) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return true;
		}

	}
	
	private BossModel createOperatorModelInfo(Operator operator) {

		BossModel modelAndView = createModel("admin/safe/info")
				.add("selectRecommends", Operator.SelectRecommend.values())
				.add("userRecommends", Operator.UserRecommend.values())
				.add("gameRecommends", Operator.GameRecommend.values())
				.add("safeRecommends", Operator.SafeRecommend.values())
				.add("moneyRecommends", Operator.MoneyRecommend.values());
		if (operator != null)
			modelAndView.add("operator", operator);
		return modelAndView;
	}
	
	/**
	 * 后期进行自己扩展 可单独写一个验证类 对于String类型的参数 不为空且不是空字符串 对于list
	 * 
	 * @param obj
	 * @return
	 */
	private boolean validation(Object obj) {
		if (obj instanceof String) {
			obj = (String) obj;
			return obj != null && !obj.equals("");
		} else {
			return false;
		}
	}
	
}
